package com.ruoyi.system.controller;

import java.util.List;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.utils.StringUtils;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import lombok.RequiredArgsConstructor;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.multipart.MultipartFile;
import com.ruoyi.system.domain.OrderType;
import com.ruoyi.system.service.IOrderTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 订单类型Controller
 *
 * @author trip
 * @date 2024-09-20
 */
@RequiredArgsConstructor(onConstructor_ = @Autowired)
@RestController
@RequestMapping("/system/ordertype")
public class OrderTypeController extends BaseController {

    private final IOrderTypeService orderTypeService;

/**
 * 查询订单类型列表
 */
@PreAuthorize("@ss.hasPermi('system:ordertype:list')")
@GetMapping("/list")
    public TableDataInfo list(OrderType orderType) {
        startPage();
        LambdaQueryWrapper<OrderType> lqw = new LambdaQueryWrapper<OrderType>();
                        if (StringUtils.isNotBlank(orderType.getOrderTypeName())){
                        lqw.like(OrderType::getOrderTypeName ,orderType.getOrderTypeName());
                    }
                        if (StringUtils.isNotBlank(orderType.getStatus())){
                        lqw.eq(OrderType::getStatus ,orderType.getStatus());
                    }
        List<OrderType> list = orderTypeService.list(lqw);
        return getDataTable(list);
    }

    /**
     * 下载导入模板
     * @param response
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil<OrderType> util = new ExcelUtil<OrderType>(OrderType.class);
        util.importTemplateExcel(response, "订单类型数据");
    }

    /**
     * 导入订单类型数据
     * @param file
     * @return
     * @throws Exception
     */
    @PreAuthorize("@ss.hasPermi('system:ordertype:import')")
    @PostMapping("/importData")
    @ResponseBody
    @Transactional(rollbackFor = Exception.class)
    public AjaxResult importData(MultipartFile file) throws Exception {
        ExcelUtil<OrderType> util = new ExcelUtil<OrderType>(OrderType.class);
        List<OrderType> list = util.importExcel(file.getInputStream());

        if (list.size() <= 0) {
            throw new RuntimeException("导入数据为空,请填充数据再导入！");
        }
        // 批量存入
            orderTypeService.saveBatch(list);

        return AjaxResult.success("导入成功");
    }

    /**
     * 导出订单类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:ordertype:export')")
    @Log(title = "订单类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, OrderType orderType) {
        List<OrderType> list = orderTypeService.selectOrderTypeList(orderType);
        ExcelUtil<OrderType> util = new ExcelUtil<OrderType>(OrderType.class);
        util.exportExcel(response, list, "订单类型数据");
    }

    /**
     * 获取订单类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:ordertype:query')")
    @GetMapping(value = "/{orderTypeId}")
    public AjaxResult getInfo(@PathVariable("orderTypeId") Long orderTypeId) {
        return AjaxResult.success(orderTypeService.selectOrderTypeByOrderTypeId(orderTypeId));
    }

    /**
     * 新增订单类型
     */
    @PreAuthorize("@ss.hasPermi('system:ordertype:add')")
    @Log(title = "订单类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody OrderType orderType) {
        return toAjax(orderTypeService.insertOrderType(orderType));
    }

    /**
     * 修改订单类型
     */
    @PreAuthorize("@ss.hasPermi('system:ordertype:edit')")
    @Log(title = "订单类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody OrderType orderType) {
        return toAjax(orderTypeService.updateOrderType(orderType));
    }

    /**
     * 删除订单类型
     */
    @PreAuthorize("@ss.hasPermi('system:ordertype:remove')")
    @Log(title = "订单类型", businessType = BusinessType.DELETE)
    @DeleteMapping("/{orderTypeIds}")
    public AjaxResult remove(@PathVariable Long[] orderTypeIds) {
        return toAjax(orderTypeService.deleteOrderTypeByOrderTypeIds(orderTypeIds));
    }
}


